<?php
declare (strict_types = 1);

namespace app\admin\model\project;

use bbweb\traits\ModelTrait;
use bbweb\basic\BaseModel;

/**
 * @mixin \think\Model
 */
class ProjectColumns extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'project_columns';

    use ModelTrait;


    // 设置字段信息
    protected $schema = [
        'id'			=> 'int',
        'title'		    => 'varchar',
        'project_id'    => 'int',
        'position'		=> 'tinyint',
        'update_at'     => 'int',
    ];

    /**
     * @param $project_id
     * @param array $columns
     * @return bool
     */
    public static function addColumn($project_id, array $columns)
    {
        $position = 0;
        foreach ($columns as $column) {
            $values = [
                'title'       => $column,
                'position'    => ++$position,
                'project_id'  => $project_id,
                'update_at'  => time(),
            ];
            if (!self::create($values)) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param $project_id
     * @return array
     */
    public static function getColumn($project_id)
    {
        return self::where('project_id', $project_id)->order('position ASC')->column('id,title,project_id,position', 'position');
    }
}
